package com.ruoyi.bizsys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
    import com.ruoyi.bizsys.mapper.AppDeviceInfoMapper;
import com.ruoyi.bizsys.domain.AppDeviceInfo;
import com.ruoyi.bizsys.service.IAppDeviceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 用户设备信息Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
public class AppDeviceInfoServiceImpl extends ServiceImpl<AppDeviceInfoMapper,AppDeviceInfo> implements IAppDeviceInfoService {


    @Autowired
    RedisCache redisCache;

    private final static String USER_DEVICE_KEY = "user_device_id:";

    /**
     * 查询用户设备信息
     *
     * @param id 用户设备信息ID
     * @return 用户设备信息
     */
    @Override
    public AppDeviceInfo selectAppDeviceInfoById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询用户设备信息列表
     *
     * @param appDeviceInfo 用户设备信息
     * @return 用户设备信息
     */
    @Override
    public List<AppDeviceInfo> selectAppDeviceInfoList(AppDeviceInfo appDeviceInfo) {
        return this.list(new QueryWrapper<>(appDeviceInfo).last("order by id desc"));
    }

    /**
     * 新增用户设备信息
     *
     * @param appDeviceInfo 用户设备信息
     * @return 结果
     */
            @Override
    public int insertAppDeviceInfo(AppDeviceInfo appDeviceInfo) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    appDeviceInfo.setCreateTime(DateUtils.getNowDate());
                                                                                                                                                                                            return baseMapper.insert(appDeviceInfo);
    }

    /**
     * 修改用户设备信息
     *
     * @param appDeviceInfo 用户设备信息
     * @return 结果
     */
            @Override
    public int updateAppDeviceInfo(AppDeviceInfo appDeviceInfo) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return baseMapper.updateById(appDeviceInfo);
    }

    /**
     * 批量删除用户设备信息
     *
     * @param ids 需要删除的用户设备信息ID
     * @return 结果
     */
            @Override
    public int deleteAppDeviceInfoByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除用户设备信息信息
     *
     * @param id 用户设备信息ID
     * @return 结果
     */
    @Override
    public int deleteAppDeviceInfoById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public String getDeviceIdByUserId(String userId, Date date) {
        Object cacheObject = redisCache.getCacheObject(USER_DEVICE_KEY+userId);
        if(cacheObject==null || (cacheObject+"").equals("")){
            //为空  重新查询
            QueryWrapper<AppDeviceInfo> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("user_id",userId);
            objectQueryWrapper.le("create_time",DateUtils.formatDate(date,"yyyy-MM-dd HH:mm:ss"));
            objectQueryWrapper.last("order by id desc limit 1");
            AppDeviceInfo appDeviceInfo = baseMapper.selectOne(objectQueryWrapper);
            String deviceId = "";
            if(StringUtils.isNotBlank(appDeviceInfo.getNewFingerprint())){
                deviceId = appDeviceInfo.getNewFingerprint();
            }else{
                deviceId = appDeviceInfo.getFingerprint();
            }
            redisCache.setCacheObject(USER_DEVICE_KEY+userId,deviceId,10, TimeUnit.SECONDS);
            return deviceId;
        }else{
            return cacheObject+"";
        }
    }

    @Override
    public String getDeviceIdByActionKey(String actionKey) {
        QueryWrapper<AppDeviceInfo> queryWrapper = new QueryWrapper<AppDeviceInfo>().eq("action_key", actionKey)
                .select("new_fingerprint", "fingerprint").last("order by id desc limit 1");
        AppDeviceInfo one = getOne(queryWrapper);
        if (Func.isNotEmpty(one)) {
            if (StringUtils.isNotBlank(one.getNewFingerprint())) {
                return one.getNewFingerprint();
            }
            return one.getFingerprint();
        }
        return null;
    }


}
